package com.example.guessthesoundtrack;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class XmlParser {
	public static XmlPullParser toXmlParser(InputStream in) throws XmlPullParserException {
		XmlPullParser parser = Xml.newPullParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    	parser.setInput(in, "UTF-8");
		return parser;
	}
	
	public static ArrayList<Level> getLevels(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Level> levels = new ArrayList<Level>();

		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Levels");

    	while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}

    		String tag = parser.getName();
    		if (tag.equals("Level")) {
    			levels.add(parseLevel(parser));
    		}
    	}
		
		return levels;
	}
	
	public static Level parseLevel(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Level");

		String movie = parser.getAttributeValue(0);
		String song = parser.getAttributeValue(1);
		
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
		}

		return new Level(movie, song);
	}
}